// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

#pragma once

#include <float.h>
#include <stdint.h>

#include <cstring>
#include <string>

namespace doris {

typedef unsigned char uchar;

#define int1store(T, A) *((uint8_t*)(T)) = (uint8_t)(A)
#define int2store(T, A) *((uint16_t*)(T)) = (uint16_t)(A)
#define int3store(T, A)                           \
    do {                                          \
        *(T) = (uchar)((A));                      \
        *(T + 1) = (uchar)(((uint32_t)(A) >> 8)); \
        *(T + 2) = (uchar)(((A) >> 16));          \
    } while (0)
#define int4store(T, A) *((uint32_t*)(T)) = (uint32_t)(A)
#define int8store(T, A) *((int64_t*)(T)) = (uint64_t)(A)
#define float4store(T, A) *((float*)(T)) = (float)(A)
#define float8store(T, A) *((double*)(T)) = (double)(A)

#define MY_ALIGN(A, L) (((A) + (L)-1) & ~((L)-1))
#define SIZEOF_CHARP 8

#define MAX_TINYINT_WIDTH 3     /* Max width for a TINY w.o. sign */
#define MAX_SMALLINT_WIDTH 5    /* Max width for a SHORT w.o. sign */
#define MAX_MEDIUMINT_WIDTH 8   /* Max width for a INT24 w.o. sign */
#define MAX_INT_WIDTH 10        /* Max width for a LONG w.o. sign */
#define MAX_BIGINT_WIDTH 20     /* Max width for a LONGLONG */
#define MAX_LARGEINT_WIDTH 39   /* Max width for a LARGEINT */
#define MAX_CHAR_WIDTH 255      /* Max length for a CHAR column */
#define MAX_BLOB_WIDTH 16777216 /* Default width for blob */
#define MAX_TIME_WIDTH 10       /* Max width for a TIME HH:MM:SS*/
#define MAX_DECPT_FOR_F_FORMAT DBL_DIG
#define MAX_DATETIME_WIDTH 27 /* YYYY-MM-DD HH:MM:SS.ssssss */
#define MAX_DECIMAL_WIDTH 29  /* Max width for a DECIMAL */

/* -[digits].E+## */
#define MAX_FLOAT_STR_LENGTH 24 // see gutil/strings/numbers.h kFloatToBufferSize
/* -[digits].E+### */
#define MAX_DOUBLE_STR_LENGTH 32 // see gutil/strings/numbers.h kDoubleToBufferSize

/* -[digits].[frac] */
#define MAX_DECIMAL_STR_LENGTH 29

inline void direct_write_to_mysql_result_string(std::string& mysql_rows, const char* str,
                                                size_t size) {
    // MySQL protocol length encoding:
    // <= 250: 1 byte for length
    // < 65536: 1 byte (252) + 2 bytes for length
    // < 16777216: 1 byte (253) + 3 bytes for length
    // >= 16777216: 1 byte (254) + 8 bytes for length

    char buf[16];
    if (size < 251ULL) {
        int1store(buf, size);
        mysql_rows.append(buf, 1);
    } else if (size < 65536ULL) {
        buf[0] = static_cast<char>(252);
        uint16_t temp16 = static_cast<uint16_t>(size);
        memcpy(buf + 1, &temp16, sizeof(temp16));
        mysql_rows.append(buf, 3);
    } else if (size < 16777216ULL) {
        buf[0] = static_cast<char>(253);
        int3store(buf + 1, size);
        mysql_rows.append(buf, 4);
    } else {
        buf[0] = static_cast<char>(254);
        uint64_t temp64 = static_cast<uint64_t>(size);
        memcpy(buf + 1, &temp64, sizeof(temp64));
        mysql_rows.append(buf, 9);
    }

    // Append string content
    mysql_rows.append(str, size);
}

inline void direct_write_to_mysql_result_null(std::string& mysql_rows) {
    // MySQL protocol for NULL value is a single byte with value 251
    mysql_rows.push_back(static_cast<char>(251));
}

} // namespace doris
